﻿using AutoMapper;
using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Write.API.Applications.Commands.SourceCommands;
using MediatR;

namespace B.R.RabbitRoot.Write.API.Applications.Commandhandlers.SourceCommandhandelrs
{
    /// <summary>
    /// 修改命令处理
    /// </summary>
    public class SoureceUpdateCommandHandler : IRequestHandler<SoureceUpdateCommand, APIResult<int>>
    {
        private readonly IBaserepository<SourceInfo> sourceinfoRedsitoty;
        private readonly IMapper mapper;
        public SoureceUpdateCommandHandler(IBaserepository<SourceInfo> sourceinfoRedsitoty,IMapper mapper)
        {
            this.sourceinfoRedsitoty = sourceinfoRedsitoty;
            this.mapper = mapper;
        }

        /// <summary>
        /// 修改命令处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<int>> Handle(SoureceUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.Code=ResultCode.修改成功;
            result.Message = ResultCode.修改成功.ToString();
           var entity=mapper.Map<SourceInfo>(request);
            entity.SourceUpdatePerson = request.AccountNickname;
            entity.SourceCreamTime = DateTime.Now;
            sourceinfoRedsitoty.UpdateAsync(entity);
            return Task.FromResult(result);
        }
    }
}
